package popcorn.ticket.serviceimpl;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import popcorn.ticket.dao.MoviesMapper;
import popcorn.ticket.entity.Movies;
import popcorn.ticket.entity.MoviesExample;
import popcorn.ticket.service.MovieService;
import popcorn.ticket.tool.PageData;

@Service
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED)
public class MovieServiceImpl implements MovieService {

	@Autowired
	MoviesMapper mm;
	
	Logger logger = Logger.getLogger(MovieServiceImpl.class);

	@Override
	public long countByExample(MoviesExample example) {
		// TODO Auto-generated method stub
		return mm.countByExample(example);
	}

	@Override
	public int insert(Movies record) {
		// TODO Auto-generated method stub
		return mm.insert(record);
	}

	@Override
	public PageData<Movies> selectByExample(MoviesExample example, int page, int pagesize) {
		PageHelper.startPage(page, pagesize);
		List<Movies> list = mm.selectByExample(example);

		PageInfo<Movies> pi = new PageInfo<Movies>(list);
		PageData<Movies> pd = new PageData<Movies>(list, pagesize, (int) pi.getTotal(), pagesize);
		return pd;
	}

	@Override
	public List<Movies> selectByExample(MoviesExample example) {
		// TODO Auto-generated method stub
		return mm.selectByExample(example);
	}

	@Override
	public Movies selectByPrimaryKey(Integer movieid) {
		// TODO Auto-generated method stub
		return mm.selectByPrimaryKey(movieid);
	}

	@Override
	public int updateByPrimaryKeySelective(Movies record) {
		// TODO Auto-generated method stub
		return mm.updateByPrimaryKeySelective(record);
	}

	@Override
	public int updateByExampleSelective(Movies record, MoviesExample example) {
		// TODO Auto-generated method stub
		return mm.updateByExampleSelective(record, example);
	}

	// 获取今天推荐的影片
	@Override
	public List<Movies> getMoviesOK() {
		// TODO Auto-generated method stub
		return mm.selectMoviesOK();
	}

	// 通过电影编号查询该电影的票数
	@Override
	public int getMTicketNum(int movieId) {
		// TODO Auto-generated method stub
		return mm.selMTicketNum(movieId);
	}

	// 通过电影编号查询该电影的类型
	@Override
	public List<Movies> getMType(int movieId) {
		// 获取电影类型
		List<Movies> list = mm.selMType(movieId);

		return list;
	}

	// 查询今日排行的电影
	@Override
	public List<Movies> getMoviesToday() {
		// TODO Auto-generated method stub
		return mm.selMoviesToday();
	}

	// 今日放映的电影（前四名）
	@Override
	public List<Movies> getMoviesTodayFour() {
		// 对电影的简介做处理
		List<Movies> list = mm.selMoviesTodayFour();
		// 遍历
		for (int i = 0; i < list.size(); i++) {
			String info = list.get(i).getMovieinfo();
			if (info.length() > 34) {
				// 取35个字符
				info = info.substring(0, 34) + "...";
			}
			// 存值
			list.get(i).setMovieinfo(info);
		}

		return list;
	}

	// 通过编号查电影的分数
	@Override
	public List<Movies> getMScore(int movieId) {
		// TODO Auto-generated method stub
		return mm.selMScore(movieId);
	}

	@Override
	public List<Movies> selMoviesByTypename(String typename) {

		List<Movies> list = mm.selMoviesByTypename(typename);
		return list;
	}

	@Override
	public List<Movies> selMoviesByLike(Movies movie) {
		// TODO Auto-generated method stub
		return mm.selMoviesByLike(movie);
	}

	@Override
	public List<Movies> selMoviesByOrder(String factor) {
		// TODO Auto-generated method stub
		return mm.selMoviesByOrder(factor);
	}

	@Override
	public PageData<Movies> getListByOrder(int page) {
		PageHelper.startPage(page, 20);
		List<Movies> list = mm.getListByOrder();
		PageInfo<Movies> pageInfo = new PageInfo<>(list);
		PageData<Movies> pd = new PageData<>(list, page, (int) pageInfo.getTotal(), 20);
		pd.setCode(0);
		pd.setMsg("");
		return pd;
	}

	// 通过电影编号查询电影详细信息
	@Override
	public Movies getMInfo(int movieId) {

		return mm.selMInfo(movieId);
	}

	/**
	 * 联想词，去重
	 */
	@Override
	public List getListWithSearch(Movies movies) {
		List result = new ArrayList<>();
		
		List<Movies> list = mm.getMoviesWithSearrch(movies);
		// 将查询出来的值重复的去掉
		if (movies.getLanguage() != null) {
			for (int i = 0; i < list.size(); i++) {
				if (!result.contains(list.get(i).getLanguage())) {
					result.add(list.get(i).getLanguage());
				}
			}
		} else if (movies.getDirector() != null) {
			for (int i = 0; i < list.size(); i++) {
				if (!result.contains(list.get(i).getDirector())) {
					result.add(list.get(i).getDirector());
				}
			}
		} else if (movies.getPlace() != null) {
			for (int i = 0; i < list.size(); i++) {
				if (!result.contains(list.get(i).getPlace())) {
					result.add(list.get(i).getPlace());
				}
			}
		} else if (movies.getMainactor() != null) {
			for (int i = 0; i < list.size(); i++) {
				if (!result.contains(list.get(i).getMainactor())) {
					result.add(list.get(i).getMainactor());
				}
			}
		} else if (movies.getMoviename() != null) {
			for (int i = 0; i < list.size(); i++) {
				if (!result.contains(list.get(i).getMoviename())) {
					result.add(list.get(i).getMoviename());
				}
			}
		} else if (movies.getmTypeName() != null) {
			logger.info("in");
			// 去掉空值
			List nullList = new ArrayList();
			nullList.add(null);
			list.removeAll(nullList);
			logger.info("lll:"+list);
			for (int i = 0; i < list.size(); i++) {
				if (!result.contains(list.get(i).getmTypeName())) {
					result.add(list.get(i).getmTypeName());
				}

			}
		}

		return result;
	}

	// 与联想词搜索相关的全部电影查询
	@Override
	public PageData<Movies> selectMoviesByLike(int page, String keywords, Movies movies) {
		PageHelper.startPage(page, 3);
		List<Movies> list = mm.selectMoviesByLike(keywords, movies);
		PageInfo<Movies> pageInfo = new PageInfo<>(list);
		PageData<Movies> pd = new PageData<>(list, page, (int) pageInfo.getTotal(), 3);
		pd.setCode(200);
		pd.setMsg("");
		return pd;
	}

	// 与联想词搜索相关的全部电影类型查询
	@Override
	public PageData<Movies> selectMoviesByLikeWTN(int page, String keywords, Movies movies) {
		PageHelper.startPage(page, 3);
		List<Movies> list = mm.selectMoviesByLikeWithoutMn(keywords, movies);
		PageInfo<Movies> pageInfo = new PageInfo<>(list);
		PageData<Movies> pd = new PageData<>(list, page, (int) pageInfo.getTotal(), 3);
		pd.setCode(200);
		pd.setMsg("");
		return pd;
	}

	// 与联想词搜索相关的全部电影查询+电影状态的查询
	@Override
	public PageData<Movies> getMLikeAndStatus(int page, String keywords, Movies movies, int moviestatus) {
		// 开启分页
		PageHelper.startPage(page, 3);
		// 调用dao
		List<Movies> list = mm.selMLikeAndStatus(keywords, movies, moviestatus);
		PageInfo<Movies> pageInfo = new PageInfo<>(list);
		PageData<Movies> pd = new PageData<>(list, page, (int) pageInfo.getTotal(), 3);
		pd.setCode(200);
		pd.setMsg("");
		return pd;
	}

	// 与联想词搜索相关的全部电影查询，与电影类型有关+状态的查询
	@Override
	public PageData<Movies> getMtypeLikeAndStatus(int page, String keywords, Movies movies, int moviestatus) {
		// TODO Auto-generated method stub
		PageHelper.startPage(page, 3);
		// 调用dao
		List<Movies> list = mm.selMtypeLikeAndStatus(keywords, movies, moviestatus);
		PageInfo<Movies> pageInfo = new PageInfo<>(list);
		PageData<Movies> pd = new PageData<>(list, page, (int) pageInfo.getTotal(), 3);
		pd.setCode(200);
		pd.setMsg("");
		return pd;
	}
	
	//获取即将上映的评分排行榜
	@Override
	public List<Movies> getMoviesSoon() {
		// TODO Auto-generated method stub
		return mm.selMoviesSoon();
	}


}
